package com.gitee.faster.redis.command;

import com.gitee.faster.redis.protocol.RedisRequest;
import com.gitee.faster.redis.protocol.RedisResponse;
import com.gitee.faster.redis.storage.RedisDataStorage;
import redis.clients.jedis.Protocol;

import java.util.List;

/**
 * @author hongda.li
 */
public class ClientCommand implements RedisCommand {
    @Override
    public Protocol.Command command() {
        return Protocol.Command.CLIENT;
    }

    @Override
    public List<Protocol.Keyword> keywords() {
        return List.of(Protocol.Keyword.GETNAME, Protocol.Keyword.SETNAME);
    }

    @Override
    public RedisResponse execute(RedisRequest request, List<String> arguments) {

        RedisDataStorage storage = RedisDataStorage.getInstance();

        Protocol.Keyword keyword = parseKeyword(arguments, 0);
        return switch (keyword) {
            case SETNAME -> {
                storage.store("client-name:" + request.getSessionId(), arguments.get(2));
                yield new RedisResponse().addItem("OK");
            }
            case GETNAME -> new RedisResponse().addItem(storage.query("client-name:" + request.getSessionId()));
            default -> new RedisResponse().addItem("Error");
        };
    }
}
